package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.AdminStatus;
import com.example.forum.common.enums.ResultCode;
import com.example.forum.common.enums.UserStatus;
import com.example.forum.common.validation.ValidationUtils;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hanzishuai
 * @date 2025/4/30 11:32
 * @Description
 */
@Tag(name = "管理员接口")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;

    private boolean isAdmin(User user) {
        if (user.getIsAdmin() != AdminStatus.IS_ADMIN.getStatus()) {
            return false;
        }
        return true;
    }


    @Operation(summary = "删除板块")
    @PostMapping("/deleteBoard")
    public AppResult deleteBoard(HttpServletRequest request,
                                 @Parameter(description = "板块 Id") @RequestParam("boardId") @NonNull Long boardId) {

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (!isAdmin(user)) {
            log.warn("{}", ResultCode.FAILED_USER_NOT_ADMIN);
            return AppResult.failed(ResultCode.FAILED_USER_NOT_ADMIN);
        }

        ValidationUtils.id(boardId, "板块 Id");
        adminService.deleteBoard(boardId);

        return AppResult.success();
    }

    @Operation(summary = "新增板块")
    @PostMapping("/addBoard")
    public AppResult addBoard(HttpServletRequest request,
                              @Parameter(description = "板块名") @RequestParam("name") @NonNull String name,
                              @Parameter(description = "排序数字") @RequestParam("sort") @NonNull Integer sort) {

        // 校验当前登录用户是否为管理员
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        if (!isAdmin(user)) {
            log.warn("{}", ResultCode.FAILED_USER_NOT_ADMIN);
            return AppResult.failed(ResultCode.FAILED_USER_NOT_ADMIN);
        }
        if(user.getState() == UserStatus.IS_BANNED.getCode()) {
            log.warn("{}", ResultCode.FAILED_USER_BANNED);
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        if(!StringUtils.hasLength(name) || sort < 0) {
            log.warn("{}", ResultCode.FAILED_PARAMS_VALIDATE);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        // 构建板块对象
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);

        // 调用 service
        adminService.addBoard(board);

        return AppResult.success();
    }


}
